/**
 * Created by 10594 on 2019/9/13.
 */
// 存在问题：只要用户名和密码存在，不搞对不对应都会成功。（已解决）
    //② 没有包装函数，代码不够简介。
    var $box_img = $('.box').children(":first");
    var $ImgSrcO ="./static/assets/img/rabbit.jpg";
    var $UserName = "";
    var $PassWord = "";
$(function ($) {
    $.getJSON("./static/assets/vendors/Json/login2.json",function (data) {
        var data = data;
        $.each(data,function (key,val) {
            if($('.UserName').val() == val["userName"])
            {
                $box_img.attr("src",val["url"]);
            }
        })
    });


    $('.UserName').on('blur',function () {
        $UserName = $(this).val();
        $.getJSON("./static/assets/vendors/Json/login2.json", function (data) {
            var data = data;
            $.each(data,function (key,val) {
                var userNametest2 = val["userName"];
                if ($UserName === val["userName"]){
                    if ($box_img.attr("src") === val["url"])
                    {
                        return false;
                    }else {
                        $box_img.fadeOut(function () {
                            $(this).on('load', function () {
                                $(this).fadeIn()
                            }).attr("src", val["url"]);
                            return false;
                        })
                    }
                }
                else{
                    if ($box_img.attr("src") === $ImgSrcO)
                    {
                       return false;
                    }
                    else {
                        $box_img.fadeOut(function () {
                            $(this).on('load', function () {
                                $(this).fadeIn()
                            }).attr("src", $ImgSrcO);
                        })
                    }
                }
            })
        })
    })
    $('.btn').on('click',function () {
        $.getJSON("./static/assets/vendors/Json/login2.json", function (data) {
            var data = data;
            $UserName = $('.UserName').val();
            $PassWord = $('.PassWord').val();
            if ($UserName === '') {
                alert("请输入用户名")
            }
            else if ($PassWord === '') {
                alert("请输入密码")
            }
            else {
            $.each(data, function (key,val) {
                if ($UserName === val["userName"]) {
                    if($PassWord !== val["passWord"]) {
                        alert("密码错误");
                    }
                    else{
                        window.location = "./index.html";
                        alert("成功了")
                    }
                    return false
                }
                else if(key === data.length-1){
                    alert("用户不存在");
                }
            })
        }

        });

    })


    
});